<?

/*
*
*   Excalibur Content Management System
*   Copyright © 2008 Egor "Sontan" Kuryanovich
*
*   Based on Explay Engine v2.0 by Golovdinov Alexander
*
*   Official site: www.excms.ru
*   Contact e-mail: support@excms.ru
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
*/

if ($EXCMS != "work") {
	header ("Location: ../../index.php");
	exit;
}


if (isset($_GET['cat']) && $_GET['cat'] == "online") 
{
	$get = $db->query ("SELECT uid, usr.user_name, usr.user_karma FROM ".DB_PEREFIX."_sessions AS sess
		LEFT JOIN (SELECT user_id, user_name, user_karma FROM ".DB_PEREFIX."_users) AS usr ON usr.user_id = sess.uid
		WHERE uid != 0 AND ugroup = 'user' ORDER BY utime DESC");
	
	if ($db->num_rows($get) > 0) 
	{
		$karmas = array ();
		$users = array ();
		while (list($uid, $name, $karma) = $db->fetch_row ($get)) 
		{
			if ($karma < 0) 
				$karma = 0;

			array_push ($karmas, $karma);
			array_push ($users, array ($uid, $name));
		}
		
		for ($i=0; $i<count($users); $i++) 
		{
			$karma = $karmas[$i];
			list ($uid, $name) = $users[$i];
			
			$size = 31;
			for ($c=0; $c<count($users); $c++) if ($karmas[$c] > $karma) $size -= 3;
			
			$list .= "\n<a href=\"/users/$uid.html\" style=\"font-size: {$size}px;\">$name</a> ";
		}
		
		past_table ('Пользователи on-line', $list);

	}
	else site_error ('На сайте нет пользователей!');
} 
else 
{
	$number = 20;
	$start = 0;
	if (isset($_GET['page']) && $_GET['page'] != '0') 
		$start = (intval ($_GET['page']) - 1) * $number;
	
	
	$get_users = $db->query ("SELECT user_id, user_name, user_date, user_karma 
		FROM  ".DB_PEREFIX."_users 
		WHERE user_ban = 'off' ORDER BY user_karma DESC LIMIT $start, $number");
	
	$count_users = $db->num_rows ($db->query ("SELECT * FROM ".DB_PEREFIX."_users WHERE user_ban = 'off'"));
	
	$content = '';
	
	while (list($id, $name, $date, $karma) = $db->fetch_row($get_users)) 
	{
		$element = get_element ('{user_li}', '{/user_li}');
		$element = str_replace ('{ID}', $id, $element);
		$element = str_replace ('{NAME}', $name, $element);
		$element = str_replace ('{DATE}', $date, $element);
		$element = str_replace ('{KARMA}', number_format($karma, 2, ',', ''), $element);
		$content .= $element;
	}
	
	$string = "";
	$get_online = $db->query ("SELECT s.utime, s.uid, s.page, u.user_name FROM ".DB_PEREFIX."_sessions AS s
		JOIN ".DB_PEREFIX."_users AS u ON (u.user_id = s.uid)
		WHERE ugroup = 'user' ORDER BY utime DESC");
	while ($u = $db->fetch_array($get_online)) 
	{
		$ago = time() - $u['utime'];
		if ($ago < 60) 
		{
			$ptime = count_smth($ago, array('секунду назад', 'секунды назад', 'секунд назад'));
		}
		elseif ($ago <= 900 && $ago >= 60) 
		{
			$ptime = count_smth( intval($ago/60), array('минуту назад', 'минуты назад', 'минут назад'));
		}
		else 
			$ptime = format_time ($u['utime']);
			
		$user_line = get_element ('{user_line}', '{/user_line}');
		$user_line = str_replace ('{ID}', $u['uid'], $user_line);
		$user_line = str_replace ('{PAGE}', $u['page'], $user_line);
		$user_line = str_replace ('{NAME}', $u['user_name'], $user_line);
		$user_line = str_replace ('{TIME}', $ptime, $user_line);
		$string .= $user_line;
	}
	
	
	echo '<table width="100%" cellspacing="0" cellpadding="4">
			<tr>
				<td valign="top" width="33%">
					Обратите внимание, эти люди находятся на сайте, или находились некоторое время назад&nbsp;&#8595;<p />
					'.$string.'
					<p align="right">
						<a href="/people/online/">все пользователи on-line</a>
					</p>
				</td>
				<td id="people" valign="top">';
	
	$template->set_var ('{HEADER}', 'Top пользователей');
	$template->set_var ('{NAME}', 'users_top');
	$block_template = $template->display ('block.tpl');
	$block_template = explode ('{BLOCK}', $block_template);
	
	echo $block_template[0];
	echo $content;
	
	if ($count_users > $number) 
		echo past_pages($start, $number, $count_users, '/people/');
	
	print $block_template[1];
	print '</td></tr></table>';

	$new = '';
	
	// Загружаем последних 20 зарегистрированных пользователей
	$get_last = $db->query ('SELECT user_id, user_name, user_date FROM '.DB_PEREFIX.'_users ORDER BY user_id DESC LIMIT 0, 20');
	while (list ($id, $name, $date) = $db->fetch_array ($get_last)) 
	{
		$new .= '<li class="friend"><a href="/users/'.$id.'.html">'.$name.'</a> <span class="help">'.format_time($date).'</span></li>';
	}
	past_table ('Новые люди', '<ul class="friends_list">'.$new.'</ul>');

}

